package games.ninJumpDx.ui.actions
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.utils.getTimer;
	
	import games.ninJumpDx.data.GameData;
	import games.ninJumpDx.data.UIConfig;
	import games.ninJumpDx.tools.Tools;
	import utils.ActionFactory;
	import games.ninJumpDx.ui.factorys.ParticleFactory;
	
	import utils.Cartoon;
	import utils.MathTools;
	import utils.particles.Particle;
	
	public class SparkParticles extends Cartoon
	{
		/*显示粒子数*/
		private var m_total:Number;
		/*粒子显示距离*/
		private var m_dist:Number;
		/*显示粒子的位图数据*/
		private var m_bmd:BitmapData;
		/*粒子列表*/
		private var m_particles:Vector.<Particle>;
		/*跟踪目标*/
		private var m_target:DisplayObject;
		/*粒子生成器*/
		private var m_particleMaker:ActionFactory;
		
		public function SparkParticles(name:String,n:Number = 20,dist:Number = 150)
		{
			super();
			m_particles= new Vector.<Particle>();
			m_total = n;
			m_dist = dist;
			m_bmd = Tools.getBitmapDataByName(name,GFrame.MAIN_UI);
			m_particleMaker = new ParticleFactory();
			
		}
		
		override public function update(t:Object=null):void
		{
			if(!start || pause)return;
			var particle:Particle = Particle(m_particleMaker.makeAction());
			
			var bm:Bitmap = new Bitmap(m_bmd);
			particle.addChild(bm);
			bm.x = -bm.width /2;
			bm.y = -bm.height /2
			particle.fade = 1.1;
			// give it a random velocity 
			particle.setVel(MathTools.randRange(-2,+2),MathTools.randRange(3,5));
			
			// add drag;
			particle.drag = 0.96;
			
			// change size
			particle.setScale( Math.random());
			
			// init place
			initParticle(particle);
			
			//particle.shrink = .95 + .05 * Math.random();
			//particle.clip.scaleX = particle.clip.scaleY = Math.random() ;
			//gravity
			particle.gravity = .6;
			
			// fade out
			//particle.fade = 0.03;
			
			// rotate
			particle.directionRotate = true;
			
			m_particles.push(particle);
			addChild(particle);
			while(m_particles.length > 15)
			{
				particle = Particle(m_particles.shift());
				particle.destroy();
			
			}
		}
		
		override public function destroy(e:* = null):void
		{
			super.destroy();
			while(numChildren > 0)
			{
				removeChildAt(0);
			}
			m_bmd.dispose();
			m_particles = null;
		}
		
		public function set target(mc:DisplayObject):void
		{
			m_target = mc;
		}
		
		
		
		private function initParticle(particle:Particle):void
		{
			particle.x = m_target.x + m_target.scaleX * m_target.width / 2 ;
			particle.y = m_target.y + 80;
			
		}
		
	
	}
}